package com.orcamento.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Value;

import com.opensymphony.xwork2.ActionSupport;
import com.orcamento.security.CookieManager;
import com.orcamento.util.AppConfig;

@SuppressWarnings("serial")
public abstract class AbstractController extends ActionSupport implements ServletResponseAware, ServletRequestAware {

	protected final String STATIC = "static";
	protected final String SUCCESS = "success";
	protected final String ERROR = "error";
	protected final String INVALID = "invalid";
	protected final String NOT_FOUND = "not_found";
	
	private String messageKey;
	private String message;
	
	private String url;
	private String urlImg;
	private String urlCss;
	private String urlJs;
	@Value("${env}")
	private String env;

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	public AbstractController() {
		setUrl( AppConfig.getUrlApplication() );
		setUrlImg( AppConfig.getImagesStaticsUrl() );
		setUrlCss( AppConfig.getCssStaticsUrl() );
		setUrlJs( AppConfig.getJavascriptStaticsUrl() );
	}

	public String getMessageType() {
		return messageKey.contains("success") && message != null ? "success": "error";
	}
	
	public String getMessageKey() {
		return messageKey;
	}

	public void setMessageKey(String messageKey) {
		setMessage( AppConfig.getProperty(messageKey) );
		this.messageKey = messageKey;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrlImg() {
		return urlImg;
	}

	public void setUrlImg(String urlImg) {
		this.urlImg = urlImg;
	}

	public String getUrlCss() {
		return urlCss;
	}

	public void setUrlCss(String urlCss) {
		this.urlCss = urlCss;
	}

	public String getUrlJs() {
		return urlJs;
	}

	public void setUrlJs(String urlJs) {
		this.urlJs = urlJs;
	}

	public String getMessage() {
		return message != null ? message : "Erro. Nenhuma mensagem para esse placeholder.";
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getEnv() {
		return env;
	}

	public void setEnv(String env) {
		this.env = env;
	}

	public Long getAccountId() {
		return CookieManager.getAccountCookie(request);
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
